package com.qwc.blog.web.admin;

import com.qwc.blog.common.annotation.AdminController;
import com.qwc.blog.common.exception.UploadFileException;
import com.qwc.blog.common.util.FileUtils;
import com.qwc.blog.config.FileProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static com.qwc.blog.common.constant.FileConstant.UPLOAD_EXCEPTION;

/**
 * @author qiuwencong
 * @Date 2022/2/23 15:53
 * @Package com.qwc.blog.web.admin
 * @Description
 */
@AdminController("upload")
public class UploadController {

    @Autowired
    private FileProperties properties;

    @PostMapping("/md/picture")
    public ResponseEntity<Object> uploadMDPicture(@RequestParam(value = "file") final MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new UploadFileException(UPLOAD_EXCEPTION);
        }
        String upload = FileUtils.upload(file, properties.getProfile(), properties.getMd());
        return ResponseEntity.ok(upload);
    }

    @PostMapping("/cover/picture")
    public ResponseEntity<Object> uploadCoverPicture(@RequestParam(value = "file") final MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new UploadFileException(UPLOAD_EXCEPTION);
        }
        String upload = FileUtils.upload(file, properties.getProfile(), properties.getCover());
        return ResponseEntity.ok(upload);
    }

    @PostMapping("/sys/picture")
    public ResponseEntity<Object> uploadSysPicture(@RequestParam(value = "file") final MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new UploadFileException(UPLOAD_EXCEPTION);
        }
        String upload = FileUtils.upload(file, properties.getProfile(), properties.getSys());
        return ResponseEntity.ok(upload);
    }
}
